#!/usr/bin/env sh
set -e

. "$(dirname -- "$0")/_/husky.sh"

if [ "$CI" = true ] ; then
  echo 'Skipping pre-commit hook'
  exit 0
fi

LOCAL_BRANCH="$(git rev-parse --abbrev-ref HEAD)"
VALID_BRANCH_NAME_REGEX="^(feat|chore|fix)\/[0-9]{1,6}(-[a-z0-9._-]{1,40})?$"

matches=`echo "$LOCAL_BRANCH" | grep -iE "$VALID_BRANCH_NAME_REGEX" | wc -l`

if [ $matches -eq 0 ]; then
  echo "There is something wrong with your branch name."
  echo "Branch names in this project must adhere to the following pattern:"
  echo "--> Regex:   $VALID_BRANCH_NAME_REGEX.\n--> Examples: \033[32mfeat/1234-example\033[0m or \033[32mchore/123\033[0m"
  echo "\nYour commit was rejected, please rename your branch to a valid name and try again."

  exit 1
fi

npm run pre-commit

exit 0
